//
//  KyHomeViewController.swift
//  KyTV
//
//  Created by fangyukui on 2017/10/20.
//  Copyright © 2017年 fangyukui. All rights reserved.
//

import UIKit

class KyHomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        automaticallyAdjustsScrollViewInsets = false
        
        view.backgroundColor = UIColor.random
        
        setupUI()

        // Do any additional setup after loading the view.
    }


}


// MARK:- 设置UI界面
extension KyHomeViewController {
    fileprivate func setupUI() {
        setupNavigationBar()
        setupPageView()
        
    }
    
    private func setupNavigationBar() {
        // 1.左侧logoItem
        let logoImage = UIImage(named: "home-logo")
        navigationItem.leftBarButtonItem = UIBarButtonItem(image: logoImage, style: .plain, target: nil, action: nil)
        
        // 2.设置右侧收藏的item
        let collectImage = UIImage(named: "search_btn_follow")
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: collectImage, style: .plain, target: self, action: #selector(followItemClick))
        // 事件监听 --> 发送消息 --> 将方法包装SEL  --> 类方法列表 --> IMP
        
        // 3.搜索框
        let searchFrame = CGRect(x: 0, y: 0, width: 200, height: 32)
        let searchBar = UISearchBar(frame: searchFrame)
        searchBar.placeholder = "主播昵称/房间号/链接"
        navigationItem.titleView = searchBar
        searchBar.searchBarStyle = .minimal
        let searchFiled = searchBar.value(forKey: "_searchField") as? UITextField
        searchFiled?.textColor = UIColor.white
    }
    
    fileprivate func loadHomeType() -> [ThemeModel]?{
        guard let path = Bundle.main.path(forResource: "types", ofType: "plist"),let titles = NSArray(contentsOfFile: path) else {
            return nil
        }
        
        var themes = [ThemeModel]()
        for dict in titles {
            let  themeM = ThemeModel(dict: dict as! [NSString : Any])
            themes.append(themeM)
        }
        return themes
        
    }
    
    fileprivate func setupPageView(){
        guard let themes = loadHomeType() else {
            return
        }
        let titles = themes.map({ $0.title })
        
        let style = KyTitleStyle()
        
        style.isScollEnble = true
        
        var childVc = [AnchorViewController]()
        
        for themeM in  themes {
            
           let story =  UIStoryboard(name: "AnchorViewController", bundle: nil)
            
           let vc  = story.instantiateInitialViewController() as! AnchorViewController
            
            vc.view.backgroundColor = UIColor.random
            
            vc.themeM = themeM
            
            childVc.append(vc)
            
        }
        
        let pageView =  KyPageView(frame: CGRect(x: 0, y: NaVBarHeight, width: view.bounds.width, height: (view.bounds.height - NaVBarHeight - TabBarHeight)), titles: titles, chirldVcs: childVc, parentVc: self, style: style)
        
        view.addSubview(pageView)
        
    }
}


// MARK:- 事件监听函数
extension KyHomeViewController {
    @objc fileprivate func followItemClick() {
        print("------")
    }
}
